﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Dotnet.Utils.Utility.Threading
{
    public static class TaskUtil
    {
        /// <summary>
        /// 长时间运行任务
        /// </summary>
        public static Task longRun(Action action)
        {
            var task = Task.Factory.StartNew(action, TaskCreationOptions.LongRunning);
            return task;
        }

        public static void WaitAll(params Action[] actions)
        {
            if (actions != null)
            {
                List<Task> list = new List<Task>();
                foreach (Action action in actions)
                {
                    list.Add(Task.Factory.StartNew(action, TaskCreationOptions.None));
                }
                Task.WaitAll(list.ToArray());
            }
        }

        public static T Retry<T>(Func<T> function, params Type[] exceptionTypes)
        {
            return Retry(function, 5, exceptionTypes);
        }

        public static T Retry<T>(Func<T> function, int retryTimes, params Type[] exceptionTypes)
        {
            Func<Type, bool> func = null;
            for (int i = 0; i < retryTimes; i++)
            {
                try
                {
                    return function();
                }
                catch (Exception ex)
                {
                    if (i == retryTimes - 1)
                    {
                        throw;
                    }
                    bool flag;
                    if (exceptionTypes.Any((p) => p == ex.GetType()))
                    {
                        if (func == null)
                        {
                            func = (p) => p != typeof(Exception);
                        }
                        flag = !exceptionTypes.All(func);
                    }
                    else
                    {
                        flag = true;
                    }
                    if (flag)
                    {
                        throw;
                    }
                    Thread.Sleep(200);
                }
            }
            return default;
        }

        public static void Retry(Action function, params Type[] exceptionTypes)
        {
            Retry(function, 5, exceptionTypes);
        }

        public static void Retry(Action function, int retryTimes, params Type[] exceptionTypes)
        {
            for (int i = 0; i < retryTimes; i++)
            {
                try
                {
                    function();
                    break;
                }
                catch (Exception ex)
                {
                    if (i == retryTimes - 1)
                    {
                        throw;
                    }
                    if (!exceptionTypes.Any((p) => p == ex.GetType()))
                    {
                        throw;
                    }
                    Thread.Sleep(200);
                }
            }
        }
    }
}